package com.leather.question.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leather.common.entity.Question;
import com.leather.common.result.Result;
import com.leather.common.toolkits.ImageKit;
import com.leather.question.service.QuestionService;
import com.leather.question.vo.QuestionAddVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class QuestionController {

    @Autowired
    private QuestionService questionService;


    @GetMapping("index")
    public Result index(int pageId) {
        return questionService.index(pageId * 15);
    }

    @PostMapping("add")
    public Result addGoods(MultipartFile image, QuestionAddVo questionAddVo) {
        String questionImgUrl = "";
        if (image != null) {
            questionImgUrl = ImageKit.uploadImage(image, "question");
        }
        questionAddVo.setQuestionImgUrl(questionImgUrl);
        return questionService.addQuestion(questionAddVo);
    }

    @PostMapping("star")
    public Result star(int questionId) {
        return questionService.star(questionId);
    }

    @PostMapping("deletequestion")
    public Result deleteQuestion(int questionId, int userId) {
        return questionService.deleteQuestion(questionId, userId);
    }

    @PostMapping("getbyid")
    public Result getQuestionById(int questionId) {
        return questionService.getQuestionById(questionId);
    }

    @PostMapping("getcount")
    public Result getCount(int userId) {
        return questionService.getCount(userId);
    }

    @GetMapping("getuserquestion")
    public Result getUserQuestion(int userId, int pageId) {
        Page<Question> page = new Page<>(pageId, 10);
        return questionService.getUserQuestion(userId, page);
    }

    @GetMapping("getinviteuser")
    public Result getInviteUser(int questionId,int userId) {
        return questionService.getInviteUser(questionId,userId);
    }

    @PostMapping("inviteuser")
    public Result inviteUser(int questionId, int userId,int inviteId) {
        return questionService.inviteUsers(questionId, userId,inviteId);
    }

}
